R, Rstudio and GitHub

Week 2 TFNC Training

Liberty Mlambo

Linking GitHub and R (Week 2)

Week 1 Recap

  • Last week we introduced Git, GitHub, R and RStudio.
  • Exercise 1: – Creating GitHub credential – Downloading and Installing R, RStudio and Git
  • Exercise 2 (Optional) - Linking GitHub and R

Configuring Git from RStudio (Exercise 2)

Configure the local Git by telling it who you are. In Rstudio Terminal:

STEP 1 : GitHub account username

First tell Git your GitHub username e.g. dzvoti999

git config --global user "dzvoti99"

STEP 2: GitHub account email address

Second tell Git your GitHub email address e.g. cer*************i@gmail.com

git config --global user.email "cer*************i@gmail.com"

STEP 3: Generate SSH Key pairs in Rstudio

To create access keys for GitHub in RStudio.

  1. Go to Global Options (from the Tools menu)
  2. Click Git/SVN
  3. Check Enable version control interface for RStudio projects

STEP 3: Generate SSH Key pairs in Rstudio…

  1. Click Create SSH Key

STEP 3: Generate SSH Key pairs in Rstudio…

  1. Click View public key
  2. Copy public key

STEP 4: Adding the SSH key to your GitHub account

  1. Go to your GitHub account in the browser
  2. Click your GitHub profile photo(right corner) then Click on settings.

STEP 4: Adding the SSH key to your GitHub account…

  1. Click SSH and GPG keys in the Access on the left side

STEP 4: Adding the SSH key to your GitHub account…

  1. Click New SSH key to add the SSH key we copied from Rstudio

STEP 4: Adding the SSH key to your GitHub account…

  1. Give your key a Title e.g. Rstudio
  2. Leave key type as Authentification Key
  3. Paste the SSH key copied from Rstudio

8. Click Add Key

GitHub workflow

Important GitHub terms

git clone

  • git clone copies/clones your remote repository(GitHub) to your local Rstudio
  • done to link a GitHub repo to Rstudio
  • only done once, when you begin working on a new project

git add

  • The git add command adds new or changed files in your working directory to the Git staging area.
  • This prepares the files for commits

git commit

  • git commitcreates a snapshot of your entire repository at specific times.
  • make new commits after every script change.
  • every commit should be accomanied by a commit message

Commit message

  • Commit messages should be short and descriptive of your change.
  • should tell a story
  • When looking through your repository changes/history you’ll be guided by the commit messages,

Git push

  • git push updates the remote repository on GitHub with local commits.
    • run git push regularly in Rstudio to stay in sync with GitHub

Git pull

  • git pull updates your current local repository branch in Rstudio from the GitHub cloud
  • run git pull regularly in Rstudio to stay in sync with GitHub

GitHub workflow

Create new repository

  • GitHub repositories store a projects e.g. TFNC-Assignments
  • Individual ownership or shared(collaborator access)
  • Repository’s visibility can be public or private

**Section adopted from https://docs.github.com/en/get-started/quickstart/create-a-repo

  1. In the upper-right corner of any page, use the drop-down menu, and select New repository.

  1. Type a short, memorable name for your repository. e.g., TFNC-Assignments
  2. Write a short description
  3. Select private visibility (to make your repo private)

  1. Check Add a ReadMe file
  2. Select the R gitignore template

  1. Select a licence

  1. Create repo

  1. Repository landing page

Clone GitHub repo to Rstudio

In GitHub

  • On your repository(TFNC-Assignments) page
  • Click code
  • Click SSH
  • Copy the link e.g. git@github.com:dzvoti/TFNC-Assignments.git

In Rstudio

  • Click New Project

  • Select Version Control

  • Select Git

  • Paste the repository url you copies from GitHub e.g.git@github.com:dzvoti/TFNC-Assignments.git`
  • Git your project directory(folder) a name (Best to use same name as repo)
  • Tell git where to place the folder

Rstudio is now synced with GitHub and you can see your files in Rstudio

GitHub workflow

Add files in Rstudio

Add files

Create a new file called First-script.R with the code below

library(tidyverse)

Stage files

  • File will appear in the Git tab

  • Check the box next to a file (stages) adds it to the stagging area

Write commit message

  • Write a suitable commit message and click commit

Commits confirmed

  • Confirm commits and click close

Git status

  • After commits git will check the remote repo and tell you that Your branch is ahead of origin/main by 1 commit

Git push

  • Click the green arrow to push changes back to github
  • Check github and you will see your files there

Exercises

Exercise 1

Clone the dzvoti/TFNC-Training repo to your local Rstudio.

Additional Resources

Additional Resources